package com.dingdang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingdang.entity.CarLabel;
import com.dingdang.entity.TbCarGoods;
import com.dingdang.service.QrqmService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service
public class QrqmServiceImpl implements QrqmService {

    @Resource
    private RestHighLevelClient client;

    //随机查询ES
    @Override
    public List<TbCarGoods> selectRandom(Integer pageNum,Integer pageSize) {
        //设置请求的对象,设置查询的索引库
        SearchRequest request = new SearchRequest("dingdangcar");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置查询的条件(这里用的是全部查询)
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        searchSourceBuilder.query(queryBuilder);
        //设置排序,默认是升序
        searchSourceBuilder.sort("sort");

        //设置分页(我只想要十条)
        searchSourceBuilder.from((pageNum - 1) * pageSize);
        searchSourceBuilder.size(pageSize);

        //将所有的请求放入request中
        request.source(searchSourceBuilder);

        //准备拿数据
        List<TbCarGoods> list = new ArrayList<>();

        try {
            //调用方法进行通讯
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            //获取结果
            SearchHit[] hits = response.getHits().getHits();
            //循环遍历
            for (SearchHit hit : hits) {
                //获取普通内容
                String sourceAsString = hit.getSourceAsString();
                //将普通内容转换成实体类类型
                TbCarGoods tbCarGoods = JSONObject.parseObject(sourceAsString, TbCarGoods.class);
                list.add(tbCarGoods);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public List<TbCarGoods> selectByLabels() {
        //从es中查询的条件
        //设置请求对象,设置查询的索引库
        SearchRequest request = new SearchRequest("dingdangcar");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        searchSourceBuilder.query(boolQueryBuilder);

        searchSourceBuilder.sort("sort");

        request.source(searchSourceBuilder);

        List<TbCarGoods> list = new ArrayList<>();

        try {
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();

            for (SearchHit hit : hits) {
                String sourceAsString = hit.getSourceAsString();
                TbCarGoods tbCarGoods = JSONObject.parseObject(sourceAsString, TbCarGoods.class);
                list.add(tbCarGoods);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
